/*
 *  gda.h
 *  greedy-data-association
 *
 *  Created by Kris Kitani on 7/26/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

class gda{
public:
	
	gda(){};
	~gda(){};
	
	void run_gda(string video_path, string csv_path, string out_path, string object_type, float score_t);
	
private:
	
	struct rect{
		double v[5];	// x,y,w,h, pose id
		int t;			// time
		Mat h;			// hold the image (histogram)
		Mat i;			// color image
		Mat m;			// binary mask
		int gid;		// incoming id
		int nomotion;	// no motion flag
		double sum_w;	// sum of width values (compute average width)
		double sum_h;
	};
	
	struct traj{ vector< vector<double> > v;};
	
	struct traj zt;							// zero trajectory
	vector <double> z;
	
	vector < struct traj > T;				// detections
	vector < struct traj > _T;
	vector < int > C;						// trajectory type
	vector < int > Tj;						//
	vector <vector<double> > logpDHjt;		// likelihood per detection
	vector <double > logpDHj;				// likelihood per trajectory
	vector <double> pc;
	
	
	double D;								// 
	int J;
	double fps;
	
	double logpH;
	double logpHD;
	double logpDH;
	int timemax;
	
	double factorial (double a);
	double getLogPs(double w);
	double getLogPss(double w,double _w, double h,double _h, int cj);
	double getLogPxx(double x, double _x,double y,double _y,int t,int _t,int cj);
	double getLogPaa(Mat &a, Mat &_a);
	double getLogPdt(int t,int _t,int cj);
	double getLogPgid(int gid,int _gid);
	double computePrior();
	void   visualizeAllTrajectories();
	void   computeLikelihood();
	void   writeToFile(string basename);
	//void   computeColorHist(Mat src, Mat mask, Mat &hist);
	void   computeColorHist_HS(Mat src, Mat mask, Mat &hist);
	void   computeColorHist_HSV(Mat src, Mat mask, Mat &hist);
	
	void   loadData(string input_file, vector <vector< struct rect > > &ped, int &timemax);
	void   loadData_csv(string input_file, vector <vector< struct rect > > &ped, int &timemax, string object_type);
	
	vector <Mat> pos;
	vector <Mat> neg;
	vector <PCA> pca;
	vector <CvDTree> dtr;
	vector <CvRTrees> rtr;
	vector <int> DA;
	vector <int> complete;
	
	Rect getTightBoundingBox(struct rect r, Mat &sm);
	void writeToFile_csv(string out_path, string object_type);
	
	int MAX_FRAMES;
	float FACTOR;
	int MIN_SAMPLES;
	float LOG_LIKELIHOOD_THRESH;
	float SCORE_THRESH;
	int PCA_DIMS;
	int PCA_ON;
	
	int T_CLOSE;
	float MIN_H;
	float MIN_W;
	
	int VIS_ON;
	int VERBOSE;
	int PAUSE;
	string METHOD;
	
	void init(string object_type, float score_t);
	
	int min_gid;						// minimum gid
	int max_gid;						// maximum gid
	
};
